package models

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
)

var StartTime int64

func Init(startTime int64) {
	StartTime = startTime
	//获取ip
	dbhost := beego.AppConfig.String("dbhost")
	//获取端口号
	dbport := beego.AppConfig.String("dbport")
	//获取用户名
	dbuser := beego.AppConfig.String("dbuser")
	//获取密码
	dbpassword := beego.AppConfig.String("dbpassword")
	//获取数据库名称
	dbname := beego.AppConfig.String("dbname")

	if dbport == "" {
		dbport = "3306"
	}

	dsn := dbuser + ":" + dbpassword + "@tcp(" + dbhost + ":" + dbport + ")/" + dbname + "?charset=utf8&loc=Asia%2FShanghai"

	//set default  db
	orm.RegisterDataBase("default", "mysql", dsn, 30)

	orm.RegisterModel(new(Admin),
		new(Auth),
		new(Ban),
		new(Role),
		new(RoleAuth),
		new(TaskServer),
		new(ServerGroup),
		new(Task),
		new(TaskGroup),
		new(TaskLog))

}

func TableName(table string) string {
	return beego.AppConfig.String("dbprefix") + table
}
